import Vue from 'vue'
import VueRouter from 'vue-router'

// import Login from '@/views/Login.vue'
const Login = () => import(/* webpackChunkName: "Login_Home_Welcome" */ '@/views/Login.vue')

// import Home from '@/views/Home.vue'
const Home = () => import(/* webpackChunkName: "Login_Home_Welcome" */ '@/views/Home.vue')

// import Welcome from '@/views/Welcome.vue'
const Welcome = () => import(/* webpackChunkName: "Login_Home_Welcome" */ '@/views/Welcome.vue')

// import Users from '@/views/user/Users.vue'
// import Rights from '@/views/power/Rights.vue'
// import Roles from '@/views/power/Roles.vue'
const Users = () => import(/* webpackChunkName: "Users_Rights" */ '@/views/user/Users.vue')
const Rights = () => import(/* webpackChunkName: "Users_Rights" */ '@/views/power/Rights.vue')
const Roles = () => import(/* webpackChunkName: "Users_Rights" */ '@/views/power/Roles.vue')

// import Category from '@/views/goods/Category.vue'
// import Params from '@/views/goods/Params.vue'
const Category = () => import(/* webpackChunkName: "Cate_Params" */ '@/views/goods/Category.vue')
const Params = () => import(/* webpackChunkName: "Cate_Params" */ '@/views/goods/Params.vue')

// import GoodsList from '@/views/goods/GoodsList.vue'
// import AddGoods from '@/views/goods/AddGoods.vue'
const GoodsList = () => import(/* webpackChunkName: "GoodsList_Add" */ '@/views/goods/GoodsList.vue')
const AddGoods = () => import(/* webpackChunkName: "GoodsList_Add" */ '@/views/goods/AddGoods.vue')

// import OrdersList from '@/views/order/OrdersList.vue'
// import Report from '@/views/report/Report.vue'
const OrdersList = () => import(/* webpackChunkName: "Order_Report" */ '@/views/order/OrdersList.vue')
const Report = () => import(/* webpackChunkName: "Order_Report" */ '@/views/report/Report.vue')

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: 'login'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        component: Welcome
      }, {
        path: '/users',
        component: Users
      }, {
        path: '/rights',
        component: Rights
      }, {
        path: '/roles',
        component: Roles
      }, {
        path: '/categories',
        component: Category
      }, {
        path: '/params',
        component: Params
      }, {
        path: '/goods',
        component: GoodsList
      }, {
        path: '/goods/add',
        component: AddGoods
      }, {
        path: '/orders',
        component: OrdersList
      }, {
        path: '/reports',
        component: Report
      }

    ]
  }
]

const router = new VueRouter({
  routes
})

/* 路由导航守卫 */
router.beforeEach((to, from, next) => {
  /* to 将要访问的路径
  from 代表从哪个路径跳转而来
  next 是一个函数，表示放行
  next() 放行， next('/login') 强制跳转 */
  if (to.path === '/login') return next()
  const token = window.sessionStorage.getItem('token')
  if (!token) return next('/login')
  next()
})
export default router
